﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ross.Service
{
    public class BaseService
    {
        /// <summary>
        /// 当前线程负责人
        /// </summary>
        public System.Security.Principal.IPrincipal oPrincipal = Thread.CurrentPrincipal;
        /// <summary>
        /// 当前用户
        /// </summary>
        public string username { get; set; }
        /// <summary>
        /// 当前年份
        /// </summary>
        public int year { get; set; } = DateTime.Now.Year;
        public BaseService()
        {
            if (oPrincipal != null)
            {
                if (oPrincipal.Identity.Name.Split(':').Length > 1)
                {
                    username = oPrincipal.Identity.Name.Split(':')[0];
                    year = int.Parse(oPrincipal.Identity.Name.Split(':')[1]);
                }
            }
        }

        public async Task<string> RemoteGet(string url)
        {            
            var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
            string result = "";
            using (var http = new HttpClient(handler))
            {
                var response = await http.GetAsync(url);
                response.EnsureSuccessStatusCode();
                result = await response.Content.ReadAsStringAsync();
            }
            return result;
        }

        /// <summary>
        /// 预算接口地址，在Web.Config中配置.
        /// </summary>
        public string BudgetServer = System.Configuration.ConfigurationManager.AppSettings["BudgetServer"];
    }
}
